﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autopark
{
    public partial class AutoparkShop : Form
    {
        public AutoparkShop()
        {
            InitializeComponent();
        }
       
        List<Cars> carList = new List<Cars>();

        private void addButton_Click(object sender, EventArgs e)
        {
            int index = shopListBox.SelectedIndex;
            switch (index)
            {
                case -1:
                    break;
                case 0:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new CityCreator();
                        carList.Add(cr.FactoryMethod("Chevrolet", "Aveo", 500));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 1:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new CityCreator();
                        carList.Add(cr.FactoryMethod("Lada", "Kalina", 400));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 2:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new SportCreator();
                        carList.Add(cr.FactoryMethod("Porsche", "911", 3000));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 3:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new SportCreator();
                        carList.Add(cr.FactoryMethod("McLaren", "F1", 2900));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 4:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new TransportationCreator();
                        carList.Add(cr.FactoryMethod("Daewoo", "Novus", 1300));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 5:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new TransportationCreator();
                        carList.Add(cr.FactoryMethod("Isuzu", "Elf", 1000));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 6:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new CleaningCreator();
                        carList.Add(cr.FactoryMethod("Multicar", "М26", 1500));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
                case 7:
                    {
                        string str = (string)shopListBox.Items[index];
                        purchaseListBox.Items.Add(str);
                        Creator cr = new CleaningCreator();
                        carList.Add(cr.FactoryMethod("Multicar", "GCX404DT", 1400));
                        Console.WriteLine(Environment.NewLine + "Машина добавлена в лист покупок");
                        break;
                    }
            }
            
            int totalPrice = 0;
            for (int i = 0; i < carList.Count; i++)
                totalPrice += carList[i]._price;         
            totalPriceLabel.Text = totalPrice.ToString();
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            int index = purchaseListBox.SelectedIndex;
            if (index != -1)
            {
                purchaseListBox.Items.Remove(purchaseListBox.Items[index]);
                carList.Remove(carList[index]);
            }
            
            int totalPrice = 0;
            for (int i = 0; i < carList.Count; i++)
                totalPrice += carList[i]._price;
            totalPriceLabel.Text = totalPrice.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter("Log.txt");
            for (int i = 0; i < carList.Count; i++)
            {
                file.WriteLine(carList[i]._brand + " " + carList[i]._model + " " + carList[i]._price);
            }
            file.Close();
            Application.Exit();
        }
    }
}
